/*
 * Classe Database
 *      Provê conexão ao banco de dados
 * Equipe:
 *      Menandro Dias
 *      Pedro Alípio
 *      Sidney dos Santos
 *      Ubirajara Gabriela
 */

package tcpclient.client;

import java.io.*;
import java.net.*;

/**
 *
 * @author menandro.neto
 */
public class TCPClient {
	Socket requestSocket;
	ObjectOutputStream out;
 	ObjectInputStream in;
 	String message;

    public TCPClient() {
        
    }

	public void run() {
		try {
			requestSocket = new Socket("localhost", 9000);
			System.out.println("Connectado a localhost na porta 9000");

            out = new ObjectOutputStream(requestSocket.getOutputStream());
			out.flush();

			in = new ObjectInputStream(requestSocket.getInputStream());
			
			do {
				try{
					message = (String)in.readObject();
					System.out.println("domPedro.tcpServer.RedesI>" + message);
					sendMessage("Inclua");
                    message = (String)in.readObject();
                    message = "fim";
				}
				catch(ClassNotFoundException classNot){
					System.err.println("Os dados recebidos, estão corrompidos.");
				}
			} while(!message.equals("fim"));
		}
		catch(UnknownHostException unknownHost){
			System.err.println("Host desconhecido!");
		}
		catch(IOException ioException){
			ioException.printStackTrace();
		}
		finally{
			try {
				in.close();
				out.close();
				requestSocket.close();
			}
			catch(IOException ioException){
				ioException.printStackTrace();
			}
		}
	}

	void sendMessage(String msg) {
		try {
			out.writeObject(msg);
			out.flush();
			System.out.println("domPedro.tcpClient.RedesI>" + msg);
		}
		catch(IOException ioException) {
			ioException.printStackTrace();
		}
	}
}
